package com.wyd.jwpkglxt_ssm.controller;

import com.wyd.jwpkglxt_ssm.entity.Department;
import com.wyd.jwpkglxt_ssm.entity.Student;
import com.wyd.jwpkglxt_ssm.service.StudentService;
import com.wyd.jwpkglxt_ssm.util.ResultCode;
import com.wyd.jwpkglxt_ssm.util.ResultUtil;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * (Student)表控制层
 *
 * @author wyd
 * @since 2021-08-04 23:10:48
 */
@RestController
@RequestMapping("student")
public class StudentController {
    /**
     * 服务对象
     */
    @Autowired
    private StudentService studentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Student selectOne(Integer id) {
        return this.studentService.queryById(id);
    }

    @GetMapping("student/page")
    public ResultUtil getAllStudentByPage(Integer pageNum, Integer pageSize,
                                          @RequestParam(required = false)String studentName,@RequestParam(required = false)Integer studentSex,
                                          @RequestParam(required = false)String studentPhone,@RequestParam(required = false)String studentSchool,
                                          @RequestParam(required = false)String studentMajor,@RequestParam(required = false)String studentGrade,
                                          @RequestParam(required = false)Integer direction,@RequestParam(required = false)String expectCity) {
        Student student = new Student().setStudentName(studentName).setStudentSex(studentSex).setStudentPhone(studentPhone).setStudentSchool(studentSchool)
                .setStudentMajor(studentMajor).setStudentGrade(studentGrade).setDirection(direction).setExpectCity(expectCity);
        System.out.println(student);
        return ResultUtil.ok().data("pageInfo",this.studentService.queryAllByPage(pageNum,pageSize,student));
    }

    @PostMapping("student")
    public ResultUtil addStudent(@RequestBody Student student){
        if(!Objects.isNull(student)){
            Student insertStudent = this.studentService.insert(student);
            if(!Objects.isNull(insertStudent)){
                return ResultUtil.ok().data("student",insertStudent);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @PutMapping("student")
    public ResultUtil updateStudent(@RequestBody Student student){
        if(!Objects.isNull(student)){
            Student updateStudent = this.studentService.update(student);
            if(!Objects.isNull(updateStudent)){
                return ResultUtil.ok().data("student",updateStudent);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @DeleteMapping("student/{studentId}")
    public ResultUtil deleteStudentById(@PathVariable("studentId")String ids){
        if(ids.contains("-")){
            String[] idsplit = ids.split("-");
            List<Integer> studentIds = new ArrayList<>();
            for(String s : idsplit){
                studentIds.add(Integer.parseInt(s));
            }
            boolean delStudents = this.studentService.deleteStudentBatch(studentIds);
            if(delStudents){
                return ResultUtil.ok();
            }
        }else if(!"".equals(ids)){
            Integer id = Integer.parseInt(ids);
            boolean delStudent = this.studentService.deleteById(id);
            if(delStudent){
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }
}